import {http,BaseUtil} from '@zkwq/business'
import {CACHE_KEY} from '../constants'



let appid = '';
let secret = '';
let redirectUri = '';
let quitRedirectUri = '';
let authHostName = '';
if(process.env.NODE_ENV == 'development'){
    appid = 'hkyx9yxiuuyzuiiwsa';
    secret = 'x1yuwzon0noyorhvbbpao0a03ppwbe9s';
    redirectUri = 'http://localhost:8081';
    quitRedirectUri = 'http://localhost:8081';
    authHostName = 'http://localhost'
}else{
    appid = 'hkylngeuv1k7f9597c';
    secret = '4dlnw6eudc5hmzv9nvra9vzsb91qknu2';
    redirectUri = 'https://yundian.stpaper.cn/';
    quitRedirectUri = 'https://yundian.stpaper.cn/';
    authHostName = 'https://yundian.stpaper.cn';
}

let magazine = null
let domain = location.hostname.split(".")[0];
// domain = "testyundian"
try{
    let result = await http.get('/yundianApi/manager/magazine/listByDomain',{domain:domain});
    console.log("22222222",result)
    if (result.code == 0 && result.data) {
        magazine = result.data
        let appClient = magazine.appClient
        if (appClient && appClient.appId) {
            appid = appClient.appId
            secret = appClient.appSecret
            redirectUri = appClient.redirectUri
            quitRedirectUri = appClient.appHome
        }
    }
}catch(e){

}
export const MAGAZINE = magazine;

export const getAuthHostName = () => {
    return authHostName;
}

export const getAccessToken = (code,callBack) => {
    if(code){
        http.get(`${location.origin}/oauth-api/oauth/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`).then(resp => {
            if(resp.failure){
                alert('登录失败,请重试')
                location.href = '/'
            }else{
                getUserInfo(resp.user_id,resp.access_token,callBack);
            }
        }).catch(e => {
            console.error(e)
        })
    }
}
export const getUserInfo = (userId,accessToken,callBack) => {
    http.get(`${location.origin}/oauth-api/scholars/${userId}/base?access_token=${accessToken}`).then(e => {
        callBack && callBack(e)
    }).catch(e => {
        console.error(e)
    });
}

export const toLogin = () => {
    BaseUtil.setStorage(CACHE_KEY.LOGIN_TO_URL,location.href)
    location.href = `https://yundian.stpaper.cn/oauth/authorizeV4?appid=${appid}&redirect_uri=${redirectUri}&scope=api_signin&response_type=code&state=STATE`
}

export const outLogin = () => {
    BaseUtil.removeStorage(CACHE_KEY.LOGIN_TO_URL)
    // var url = encodeURIComponent(`https://yundian.stpaper.cn/hky/oauth/logout?appid=${appid}&redirect_uri=${quitRedirectUri}`);
    // location.href = `https://passport.escience.cn/logout?WebServerURL=${url}`
    location.href = `https://yundian.stpaper.cn/oauth-api/oauth/logout?appid=${appid}&redirect_uri=${quitRedirectUri}`
}

export function getUrlParam(str, def) {
    var query = window.location.search.substring(1);
    query = decodeURIComponent(query);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == str) { return pair[1]; }
    }
    return def || '';
}